package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.BCrypt;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author chencfgs
 * @date 2019-12-08 11:12
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUserName(username);
        if (user == null) {
            return null;
        }
        Set<Role> roles = roleDao.findByUserId(user.getId());
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }

    //增加
    public void add(User user,Integer[] roleIds) {
        user.setRegitime(new Date());
        //用户加密
        String password = user.getPassword();
        String newPassword = BCrypt.hashpw(password,BCrypt.gensalt());
        user.setPassword(newPassword);
        userDao.add(user);
        for (Integer roleId : roleIds) {
            userDao.adduserrole(user.getId(),roleId);
        }
    }

    //删除
    public void delete(Integer id) {
        Long aLong = roleDao.findById(id);
        if (aLong!=0){
            userDao.deleteuserkey(id);
            userDao.delete(id);
        }
    }


    //编辑
    public void update(User user,Integer[] ids) {
        for (Integer id : ids) {
            userDao.updateuserkey(id,user.getId());
        }
        userDao.update(user);
    }

    //查询所有的角色信息
    @Override
    public List<Role> getAllRoles() {
        return userDao.getAllRoles();
    }


    //更改用户密码
    @Override
    public Result updateUser(Integer id, String password) {
        //密码加密
        String newPassword = BCrypt.hashpw(password,BCrypt.gensalt());
        userDao.updateUser(id,newPassword);
        return new Result(true,"更改成功");
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && queryString.length() > 0) {
            queryString = "%" + queryString + "%";
            queryPageBean.setQueryString(queryString);
        }
        List<User> list = userDao.selectByCondition(queryPageBean);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), list);
    }

}
